package com.c.yinyuezhushou.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Adapter.MusicRecAdapter;
import com.c.yinyuezhushou.Dialog.MyDialog;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.Interface.OneInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.MyObserver;
import com.c.yinyuezhushou.MRetrofit.MyRetrofit;
import com.c.yinyuezhushou.MRetrofit.PlayList;
import com.c.yinyuezhushou.MRetrofit.RetrofitService;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class HomeFragment3 extends Fragment {
    private MyApplication myApplication;
    private OneInterface oneInterface;
    private RecyclerView recyclerView;

    public HomeFragment3(OneInterface oneInterface){
        this.oneInterface = oneInterface;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home3, container, false);
        recyclerView = rootView.findViewById(R.id.fh3_rec);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myApplication = (MyApplication)getActivity().getApplicationContext();
        songList();
    }
    private void songList(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        MyRetrofit.getOb(getActivity(), RetrofitService.class).getPlaylist(3778678)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<PlayList>() {
                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull PlayList playList) {
                        if (playList!=null){
                            if (playList.getCode()==200&&playList.getPlaylist()!=null){
                                List<MusicItem> list = new ArrayList<MusicItem>();
                                int[] ints = new int[playList.getPlaylist().getTracks().size()];
                                for (PlayList.PlaylistBean.TracksBean tracksBean: playList.getPlaylist().getTracks()){
                                    MusicItem musicItem = new MusicItem();
                                    musicItem.setSong(tracksBean.getName());
                                    musicItem.setId(tracksBean.getId());
                                    musicItem.setSinger(tracksBean.getAr().get(0).getName());
                                    musicItem.setImageurl(tracksBean.getAl().getPicUrl() + "?imageView=1&thumbnail=400z400&type=webp&quality=80");
                                    list.add(musicItem);
                                }
                                MusicRecAdapter recAdapter = new MusicRecAdapter(list, myApplication.getMusicService(), getFragmentManager(), null);
                                recyclerView.setAdapter(recAdapter);
                                oneInterface.back();
                                Toast.makeText(getActivity(), "加载成功", Toast.LENGTH_SHORT).show();

                            }
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Toast.makeText(getContext(), "加载失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
